package org.qiyu.live.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Program: qiyu-live-app
 * @Description:
 * @Author: tangfh
 * @Create: 2024-07-10 14:20
 */
//@Configuration
public class CorsConfig {
    /**
     * @return org.springframework.web.cors.CorsConfiguration
     * @Description :
     * @Date 11:18 2021/2/21 0021
     * @Param * @param  ：
     **/
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许跨域的域名，可以用*表示允许任何域名使用,  在Springboot2.4对应Spring5.3后在设置allowCredentials(true)的基础上不能直接使用通配符设置allowedOrigins，而是需要指定特定的URL。如果需要设置通配符，需要通过allowedOriginPatterns指定
        corsConfiguration.addAllowedOrigin("http://localhost:63342");
//        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    /**
     * @return org.springframework.web.filter.CorsFilter
     * @Description :
     * @Date 11:19 2021/2/21 0021
     * @Param * @param  ：
     **/
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }

}

